package com.jinzhi.jzweb.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.domain.JazSysContractDO;
import com.jinzhi.jzweb.domain.JazSysContractTemplateDO;
import com.jinzhi.jzweb.domain.JazSysProjectDO;
import com.jinzhi.jzweb.domain.JazUserOrderDO;
import com.jinzhi.jzweb.service.JazSysContractService;
import com.jinzhi.jzweb.service.JazSysContractTemplateService;
import com.jinzhi.jzweb.service.JazSysProjectService;
import com.jinzhi.jzweb.service.JazUserOrderService;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Optional;

/*
 *  @author : JingChangTian
 *  @date   : 2023/7/11 15:06
 *  @description :
 *
 */
@Controller
@RequestMapping("/sysContractTemplate")
@Api(value = "合同后台管理", tags = "合同后台管理")
public class JazSysContractTemplateController {

    private final JazSysContractTemplateService jazSysContractService;

    private final JazUserOrderService jazUserOrderService;

    private final JazSysProjectService jazSysProjectService;

    public JazSysContractTemplateController(JazSysContractTemplateService jazSysContractService, JazUserOrderService jazUserOrderService, JazSysProjectService jazSysProjectService) {
        this.jazSysContractService = jazSysContractService;
        this.jazUserOrderService = jazUserOrderService;
        this.jazSysProjectService = jazSysProjectService;
    }


    @ApiOperation(value = "分页查询合同列表", notes = "分页查询合同列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "pageSize", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "curr", value = "curr", required = true, dataType = "Integer"),
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @ResponseBody
    @GetMapping(value = "/getSysContractPage")
    public Result<Page<JazSysContractTemplateDO>> getSysContractPage(@RequestParam("pageSize") Integer pageSize, @RequestParam("curr") Integer curr) {
        return Result.ok(jazSysContractService.getSysContractPage(pageSize, curr));
    }

    @Log(value = "新增合同",sysModule = 7,operationType = 1)
    @ApiOperation(value = "新增合同", notes = "新增合同")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "JazSysContractDO", value = "JazSysContractDO", required = true, dataType = "JazSysContractDO"),
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @ResponseBody
    @PostMapping(value = "/insertSysContract")
    public Result<String> insertSysContract(@RequestBody JazSysContractTemplateDO jazSysContractDO) {
        Boolean aBoolean = jazSysContractService.insert(jazSysContractDO);
        return aBoolean ? Result.ok() : Result.fail();
    }

    @Log(value = "修改合同",sysModule = 7,operationType = 3)
    @ApiOperation(value = "修改合同", notes = "修改合同")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "JazSysContractDO", value = "JazSysContractDO", required = true, dataType = "JazSysContractDO"),
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @ResponseBody
    @PostMapping(value = "/updateSysContract")
    public Result<String> updateSysContract(@RequestBody JazSysContractTemplateDO jazSysContractDO) {
        jazSysContractDO.setUpdateTime(new Date());
        Boolean aBoolean = jazSysContractService.updateById(jazSysContractDO);
        return aBoolean ? Result.ok() : Result.fail();
    }

    @Log(value = "删除合同",sysModule = 7,operationType = 2)
    @ApiOperation(value = "根据id删除合同", notes = "根据id删除合同")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long"),
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @ResponseBody
    @GetMapping(value = "/deletedSysContract")
    public Result<String> deletedSysContract(@RequestParam("id") Long id) {
        //验证有没有正在进行中的订单
        List<JazUserOrderDO> jazUserOrderDOs = jazUserOrderService.selectList(new EntityWrapper<JazUserOrderDO>().
                ne("status", 2));
        if (!jazUserOrderDOs.isEmpty()) {
            return Result.fail("有正在进行的订单无法删除");
        }
        Boolean aBoolean = jazSysContractService.deleteById(id);
        return aBoolean ? Result.ok() : Result.fail();
    }

    @ApiOperation(value = "根据id查询合同详情", notes = "根据id查询合同详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long"),
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @ResponseBody
    @GetMapping(value = "/getSysContractById")
    public Result<JazSysContractTemplateDO> getSysContractById(@RequestParam("id") Long id) {
        return Result.ok(jazSysContractService.getSysContractById(id));
    }

    @ApiOperation(value = "合同详情页面入口", notes = "合同详情页面入口")
    @ApiImplicitParams({
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @GetMapping("/info")
    public String info() {
        return "jzweb/jazSysContract/info";
    }

    @ApiOperation(value = "合同页面入口", notes = "合同页面入口")
    @ApiImplicitParams({
    })
    @GetMapping("")
    @RequiresPermissions("jzweb:jazSysContractTemplate:list")
    public String index() {
        return "jzweb/jazSysContractTemplate/index";
    }


    @GetMapping("/add")
    String add(){
        return "jzweb/jazSysContractTemplate/add";
    }
}